var callcenter = {
	ivr : null,
	serverIp : null,
	serverPort : null,
	phoneRegisted : false,
	ivrRegisted : false,
	phoneHolded : false,
	ivrBusy : true,
	ivrState : -2,
	agentNumber : null,
	agentName : null,
	extension : null,
	agentGroup : null,
	agentLevel : null,
	bBusy : null,
	bIdle : null,
	bDial : null,
	bAccept : null,
	bConf : null,
	bHold : null,
	bHangup : null,
	bMonit : null,
	bIntercept : null,
	remoteNumber : null,

	init : function(gServerIp, gServerPort, gAgentNumber, gAgentName, gExtension, gAgentGroup, gAgentLevel) {

		callcenter.serverIp = gServerIp;
		callcenter.serverPort = gServerPort;
		callcenter.agentNumber = gAgentNumber;
		callcenter.agentName = gAgentName;
		callcenter.extension = gExtension;
		callcenter.agentGroup = gAgentGroup;
		callcenter.agentLevel = gAgentLevel;
		callcenter.ivr = document.getElementById("SpzcIvrObject");

		callcenter.bBusy = $("#btnBusy");
		callcenter.bIdle = $("#btnIdle");
		callcenter.bDial = $("#btnPhoneDial");
		callcenter.bAccept = $("#btnPhoneAccept");
		callcenter.bTransfer = $("#btnPhoneTransfer");
		callcenter.bConf = $("#btnPhoneConf");
		callcenter.bHold = $("#btnPhoneHold");
		callcenter.bHangup = $("#btnPhoneHangup");
		callcenter.bMonit = $("#btnPhoneMonit");
		callcenter.bIntercept = $("#btnPhoneIntercept");
		callcenter.btnPhone = $("#btnPhoneStatus")
	},
	setBusy : function(bBusy) {// 设置是否忙碌
		if (!bBusy) {
			callcenter.ivrBusy = false;
			callcenter.ivr.SSetCancelBusy();
		} else {
			callcenter.ivrBusy = true;
			callcenter.ivr.SSetBusy();
		}
	},
	getStrStatus : function() {

		callcenter.ivrState = callcenter.ivr.MyState;
		if (callcenter.ivrState == 2)
			return "空闲";
		if (callcenter.ivrState == 3)
			return "摘机";
		if (callcenter.ivrState == 4)
			return "通话中";
		if (callcenter.ivrState == 5)
			return "暂停";
		if (callcenter.ivrState == 6)
			return "振铃中";
		if (callcenter.ivrState == 7)
			return "通话保持";
		if (callcenter.ivrState == 8)
			return "外拨通话";
		if (callcenter.ivrState == 9)
			return "正在监听";
		if (callcenter.ivrState == 10)
			return "已强插";
		if (callcenter.ivrState == 11)
			return "话后处理";
		if (callcenter.ivrState == 12)
			return "正在外拨";
		if (callcenter.ivrState == 13)
			return "三方振铃";
	},
	setLocalHold : function(bHold) {
		var btn = $("#btnPhoneHold");
		if (bHold)
			btn.text("解除");
		else
			btn.text("保持");
	},
	update : function() {
		callcenter.ivrState = callcenter.ivr.MyState;

		callcenter.ivrRegisted = (callcenter.ivrState >= 2);
		callcenter.ivrBusy = (callcenter.ivrState == 5);

		var bBusy = callcenter.bBusy;
		var bIdle = callcenter.bBusy;
		var bDial = callcenter.bBusy;
		var bAccept = callcenter.bBusy;
		var bTransfer = callcenter.bBusy;
		var bConf = callcenter.bBusy;
		var bHold = callcenter.bBusy;
		var bHangup = callcenter.bBusy;
		var bMonit = callcenter.bBusy;
		var bIntercept = callcenter.bBusy;

		var btnPhone = callcenter.btnPhone;
		if (callcenter.ivrRegisted) {
			if (callcenter.ivrBusy) {
				bBusy.addClass("disabled");
				bIdle.removeClass("disabled");
			} else {
				bBusy.removeClass("disabled");
				bIdle.addClass("disabled");
			}
		} else {
			bBusy.addClass("disabled");
			bIdle.addClass("disabled");
		}
		// pStatus.innerText="1";
		callcenter.registAll();
		// alert(ivrState );
		btnPhone.removeClass("disabled");
		if (callcenter.ivrState < 2) { // 未连接或未注册
			btnPhone.text("未注册");
			btnPhone.addClass("disabled");
			bDial.addClass("disabled");
			bAccept.addClass("disabled");
			bTransfer.addClass("disabled");
			bConf.addClass("disabled");
			bHold.addClass("disabled");
			bHangup.addClass("disabled");
			bMonit.addClass("disabled");
			bIntercept.addClass("disabled");
			// alert(ivrState );
			return;
		} else if (callcenter.ivrState == 2) { // 空闲
			btnPhone.text(callcenter.extension); // 显示分机号码

			btnPhone.removeClass("disabled");
			// pStatus.innerText="13911275013\n(北京)";
			callcenter.phoneHolded = false;
			callcenter.setLocalHold(false);
			bDial.removeClass("disabled");
			bAccept.addClass("disabled");
			bTransfer.addClass("disabled");
			bConf.addClass("disabled");
			bHold.addClass("disabled");
			bHangup.addClass("disabled");
			bMonit.removeClass("disabled");
			bIntercept.removeClass("disabled");
		} else if (callcenter.ivrState == 3 || callcenter.ivrState == 5) { // 摘机、暂停时不能使用
			btnPhone.text(callcenter.extension + "（暂停）");
			btnPhone.addClass("disabled");
			bDial.addClass("disabled");
			bAccept.addClass("disabled");
			bTransfer.addClass("disabled");
			bConf.addClass("disabled");
			bHold.addClass("disabled");
			bHangup.addClass("disabled");
			bMonit.addClass("disabled");
			bIntercept.addClass("disabled");
		} else if (callcenter.ivrState == 6 || callcenter.ivrState == 12 || callcenter.ivrState == 13 || callcenter.ivrState == 9 || callcenter.ivrState == 10 || callcenter.ivrState == 11) { // 振铃、监听、强插等
			btnPhone.text(callcenter.remoteNumber + "\n（" + callcenter.getStrStatus() + "）"); // 显示来电号码
			btnPhone.removeClass("disabled");
			bDial.addClass("disabled");
			bAccept.addClass("disabled");
			bTransfer.addClass("disabled");
			bConf.addClass("disabled");
			bHold.addClass("disabled");
			bHangup.removeClass("disabled");
			bMonit.addClass("disabled");
			bIntercept.addClass("disabled");
		} else if (ivrState == 4 || ivrState == 7 || ivrState == 8) { // 通话中、保持、外拨通话中
			btnPhone.text(callcenter.remoteNumber + "\n（" + callcenter.getStrStatus() + "）");
			btnPhone.removeClass("disabled");
			bDial.addClass("disabled");
			bAccept.addClass("disabled");
			bTransfer.removeClass("disabled");
			bConf.removeClass("disabled");
			bHold.removeClass("disabled");
			bHangup.removeClass("disabled");
			bMonit.addClass("disabled");
			bIntercept.addClass("disabled");
		}
	},

	ivrLogin : function() {
		// 登录
		callcenter.ivr.SLogin(callcenter.agentNumber, callcenter.agentName, callcenter.extension, callcenter.agentGroup, callcenter.agentLevel);
	},
	ivrConnect : function() {
		// 连接到服务器
		callcenter.ivr.SInit(callcenter.serverIp, callcenter.serverPort);
	},
	registAll : function() {
		if (!callcenter.ivrRegisted) {
			callcenter.ivrState = callcenter.ivr.MyState;
			if (callcenter.ivrState <= -1) { // 未连接
				callcenter.ivrConnect();
			} else if (callcenter.ivrState == 0 || callcenter.ivrState == 1) { // 已连接，未注册
				callcenter.ivrLogin();
			}
		}
		if (callcenter.ivrBusy && callcenter.ivr.MyState != 5) {
			callcenter.ivr.SSetBusy();
		}
	},
	phoneTransfer : function(strPhoned) {// 呼叫转移
		callcenter.remoteNumber = strPhoned;
		callcenter.ivr.STransferE(strPhoned);
	},
	phoneDial : function(strPhoned) {// 拨号
		callcenter.remoteNumber = strPhoned;
		callcenter.ivr.SDial(strPhoned, "");
	},
	phoneReceiveDial : function(strPhoned) {// 来电后
		callcenter.remoteNumber = strPhoned;
		callcenter.update();
		
		//callcenter.onPhoneReceiveDial.call();
	},
	phoneHold : function() {
		if (!callcenter.phoneHolded) {
			callcenter.ivr.SHold();
			// 解除
			phoneHolded = true;
		} else {
			callcenter.ivr.SCancelHold();
			// 保持
			callcenter.phoneHolded = false;
		}
	},
	phoneLisen : function(strAgrentId) {
		callcenter.ivr.SListenE(strAgrentId)
	}

};
